import com.aliwo.entity.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * . java8 新特性 list转换为map
 *
 * @author xuyayuan
 * @date 2021/10/8 11:09
 */
public class ListToMapDemo {

    public static List<Student> initList() {
        List<Student> list = new ArrayList<>();
        Student student1 = new Student();
        student1.setId(1L);
        student1.setUsername("xyy");
        student1.setPassword("123456");
        student1.setSex("男");
        student1.setAge(22);
        Student student2 = new Student();
        student2.setId(1L);
        student2.setUsername("xyy");
        student2.setPassword("123456");
        student2.setSex("男");
        student2.setAge(22);

        Student student3 = new Student();
        student3.setId(2L);
        student3.setUsername("zwm");
        student3.setPassword("123456");
        student3.setSex("女");
        student3.setAge(22);

        Student student4 = new Student();
        student4.setId(3L);
        student4.setUsername("zem");
        student4.setPassword("123456");
        student4.setSex("女");
        student4.setAge(29);

        Student student5 = new Student();
        student5.setId(4L);
        student5.setUsername("oop");
        student5.setPassword("654987");
        student5.setSex("女");
        student5.setAge(29);

        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        list.add(student5);

        return list;
    }

    public static void main(String[] args) {
        List<Student> list = initList();

        // 重复的键 第一种解决方式：将后面的value覆盖前面的value 或者将前面的value覆盖后面的value
        Map<Long, String> map = list.stream().collect(Collectors.toMap(Student::getId, Student::getUsername, (key1,
                                                                       key2) -> key2));
        System.out.println("重复的键 第一种解决方式 将后面的value覆盖前面的value 或者将前面的value覆盖后面的value");
        System.out.println("map:" + map);

        System.out.println("重复的键 第二种解决方式 重复时将前面的value 和后面的value拼接起来");
        // 重复的键 第二种解决方式：重复时将前面的value 和后面的value拼接起来
        Map<Long, String> map2 = list.stream().collect(Collectors.toMap(Student::getId, Student::getUsername, (key1,
                key2) -> key1 + "," + key2));
        System.out.println("map2:" + map2);

        System.out.println("重复的键 第三种解决方式 重复时将重复key的数据组成集合");
        Map<Long, List<String>> map3 = list.stream()
                                           .collect(Collectors.toMap(Student::getId,
                                                                     p -> {
                                                                         List<String> getNameList = new ArrayList<>();
                                                                         getNameList.add(p.getUsername());
                                                                         return getNameList;
                                                                     },
                                                                     (List<String> value1, List<String> value2) -> {
                                                                         value1.addAll(value2);
                                                                         return value1;
                                                                     }
                                           ));
        System.out.println(map3);


        Student student6 = new Student();
        student6.setId(5L);
        student6.setUsername(null);
        student6.setPassword("654987");
        student6.setSex("女");
        student6.setAge(29);
        list.add(student6);
        System.out.println("第二种问题报的错误如下：NullPointerException 解决方法：" +
                                   "在转换流中加上判空，即便value为空,依旧输出。(与上面方法三相同)");
        Map<Long, List<String>> map4 = list.stream()
                                           .collect(Collectors.toMap(Student::getId,
                                                                     p -> {
                                                                         List<String> getNameList = new ArrayList<>();
                                                                         getNameList.add(p.getUsername());
                                                                         return getNameList;
                                                                     },
                                                                     (List<String> value1, List<String> value2) -> {
                                                                         value1.addAll(value2);
                                                                         return value1;
                                                                     }
                                           ));
        System.out.println(map4);



    }
}
